Shader "custom/intermediate/toonShading_1" {
	Properties {
		_Color ("Color", Color) = (1,1,1,1)
	}
	SubShader {
		Pass{
			Tags { "LightMode" = "ForwardBase"}
			
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			
			//user define variable
			uniform float4 _Color;
			
			//unity define variable
			uniform float4 _LightColor0;
			
			struct vertexInput
			{
				float4 vertex:POSITION;
				float3 normal:NORMAL;
			};
			struct vertexOuput
			{
				float4 pos:SV_POSITION;
				float4 posWorld:TEXCOORD0;
				float3 normalDir:TEXCOORD1;
				float3 lightDir:TEXCOORD2;
				float   attenuation;
				//float4 color:Color;
			};
			
			vertexOuput vert(vertexInput i)
			{
				vertexOuput o;
				o.pos = mul(UNITY_MATRIX_MVP, i.vertex);
				o.posWorld = mul(_Object2World,i.vertex);
				o.normalDir = normalize(mul(float4(i.normal, 0.0), _World2Object).xyz) ; 
				
				// _WorldSpaceLightPos0.w = 0.0  => directional light
				float3 vertexToPointLightDir = _WorldSpaceLightPos0.xyz - o.posWorld.xyz;
				float mDistance = length(vertexToPointLightDir);
				o.lightDir = normalize(lerp(_WorldSpaceLightPos0.xyz, vertexToPointLightDir, _WorldSpaceLightPos0.w));
				o.attenuation = lerp(1.0,  1.0/mDistance, _WorldSpaceLightPos0.w);
				return o;	
			}
			
			float4 frag(vertexOuput o):Color
			{
				float3 diffuseLight 
						=o.attenuation 
							* _LightColor0.xyz
								* saturate(max(0.0, dot(o.normalDir, o.lightDir)) ) ;
				float3 lightFinal = diffuseLight;
				return float4(lightFinal * _Color.xyz , 1.0);
			}
			ENDCG
		}
		
		Pass{
			Tags { "LightMode" = "ForwardAdd"}
			Blend One One
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			
			//user define variable
			uniform float4 _Color;
			
			//unity define variable
			uniform float4 _LightColor0;
			
			struct vertexInput
			{
				float4 vertex:POSITION;
				float3 normal:NORMAL;
			};
			struct vertexOuput
			{
				float4 pos:SV_POSITION;
				float4 posWorld:TEXCOORD0;
				float3 normalDir:TEXCOORD1;
				float3 lightDir:TEXCOORD2;
				float   attenuation;
				//float4 color:Color;
			};
			
			vertexOuput vert(vertexInput i)
			{
				vertexOuput o;
				o.pos = mul(UNITY_MATRIX_MVP, i.vertex);
				o.posWorld = mul(_Object2World,i.vertex);
				o.normalDir = normalize(mul(float4(i.normal, 0.0), _World2Object).xyz) ; 
				
				// _WorldSpaceLightPos0.w = 0.0  => directional light
				float3 vertexToPointLightDir = _WorldSpaceLightPos0.xyz - o.posWorld.xyz;
				float mDistance = length(vertexToPointLightDir);
				o.lightDir = normalize(lerp(_WorldSpaceLightPos0.xyz, vertexToPointLightDir, _WorldSpaceLightPos0.w));
				o.attenuation = lerp(1.0,  1.0/mDistance, _WorldSpaceLightPos0.w);
				
				return o;	
			}
			
			float4 frag(vertexOuput o):Color
			{
				float3 diffuseLight 
						=o.attenuation 
							* _LightColor0.xyz
								* saturate(max(0.0, dot(o.normalDir, o.lightDir)) ) ;
				float3 lightFinal = diffuseLight;
				return float4(lightFinal * _Color.xyz , 1.0);
			}
			ENDCG
		}
	} 
	FallBack "Diffuse"
}

